<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">

<title>角色管理</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<%@ include file="/WEB-INF/jsp/common/header.jsp"%>

	<script>
		$(function() {

			bindgrid();
		});
	</script>
	<script>
		function editrow() {
			$('#tab').tabs('select', 0);
			//编辑用户
			var role = $('#tt').datagrid('getSelected');
			if (role) {
				$('#add').window('open');
				$('#t1').val(role.roleid);
				$('#t2').val(unescape(role.rolename));
				$('#t3').val(unescape(role.rolecomment));
				$('#t4').combobox('setValue', role.disabled);
				$('#cbTogether').combobox('setValue', role.istogether);
				//if (role.DISABLED == "1")
				//    $('#t4').attr('checked', true);
				//else
				//    $('#t4').attr('checked', false);
				//if (role.ISTOGETHER == "1")
				//    $('#cbTogether').attr('checked', true);
				//else
				//    $('#cbTogether').attr('checked', false);

				$('#t5').numberbox('setValue', role.rolesort);
				//填充checkbox
				bindtree();
			} else {
				$.messager.alert('消息', '请选中一个角色');
			}
		}
		function save() {
			if (!$('#ssec-form').form('validate')) {
				return;
			}
			var a = $('#t1').val();
			var b = $('#t2').val();
			var c = $('#t3').val();
			var e = $('#t5').val();
			var f = getChecked(); //权限选择
			//var d = $('#t4').attr("checked");
			//var together = $('#cbTogether').attr("checked");
			var d = $('#t4').combobox('getValue');
			var together = $('#cbTogether').combobox('getValue');
			$.ajax({
				type : "post",
				url : "RoleManage/AddRole",
				//                dataType: "xml",
				data : {
					id : a,
					name : b,
					bz : c,
					jy : d,
					xh : e,
					qx : f,
					together : together,
					czr : PUser.loginname
				},
				success : function(data) {
					$.messager.alert('消息', data);
					if (data == '保存成功!') {
						$('#add').window('close');
						bindgrid();
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert('消息', errorThrown);
				}
			});
		}

		function getChecked() {
			var val = [];
			$("input:checkbox[id*='$']:checked").each(function() {
				val.push($(this).attr('id'));
			});
			console.log('qx:'+val.join());
			return (val.join());
		}

		function del(node) {
			$.ajax({
				type : "post",
				url : "RoleManage/DelRole",
				//                dataType: "xml",
				data : {
					id : node.roleid,
					czr : PUser.loginname
				},
				success : function(data) {
					$.messager.alert('消息', data);
					bindgrid();
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert('消息', errorThrown);
				}
			});
		}
	</script>
	<script>
		function bindgrid() {
			$('#tt').datagrid(
					{
						title : '角色列表',
						url : 'RoleManage/RoleListJson',
						iconCls : 'icon-applicationgo',
						nowrap : true,
						rownumbers : true,
						animate : false,
						collapsible : false,
						singleSelect : true,
						remoteSort : false,
						width : 1000,
						height : 500,
						columns : [ [ {
							field : 'roleid',
							title : '角色ID',
							hidden : true,
							sortable : true,
							width : 100,
							formatter : function(val, rec) {
								return unescape(val)
							}
						}, {
							field : 'rolename',
							title : '角色名称',
							sortable : true,
							width : 200,
							formatter : function(val, rec) {
								return unescape(val)
							}
						}, {
							field : 'rolecomment',
							title : '备注',
							width : 150,
							formatter : function(val, rec) {
								return unescape(val)
							}
						}, {
							field : 'disabled',
							title : '启用禁用',
							width : 60,
							formatter : function(val, rec) {
								if (val == '1')
									return '启用';
								else
									return '禁用';
							}
						}, {
							field : 'istogether',
							title : '是否共存',
							width : 60,
							formatter : function(val, rec) {
								if (val == '0')
									return '否';
								else
									return '是';
							}
						}, {
							field : 'rolesort',
							title : '排序',
							width : 50,
							sortable : true,
							formatter : function(val, rec) {
								return unescape(val)
							}
						}

						] ],
						toolbar : [
								{
									text : '增加角色',
									iconCls : 'icon-add',
									handler : function() {
										//增加用户
										$('#ssec-form').form('clear');
										$('#add').window('open');
										$('#t4').combobox('setValue', '1');
										$('#cbTogether').combobox('setValue',
												'1');
										bindtree();
									}
								},
								'-',
								{
									text : '修改角色/授权',
									iconCls : 'icon-edit',
									handler : function() {
										editrow();
									}
								},
								'-',
								{
									text : '删除角色',
									iconCls : 'icon-cancel',
									handler : function() {
										//删除用户
										var role = $('#tt').datagrid(
												'getSelected');
										if (role) {
											$.messager.confirm('提示', '确定要删除【'
													+ unescape(role.rolename)
													+ '】这个角色吗?', function(r) {
												if (r) {
													del(role);
												}
											});
										} else
											$.messager.alert('消息', '请选中一个角色');
									}
								} ],
						onLoadSuccess : function() {

						},
						onDblClickRow : function() {
							editrow();
						}
					});
		}
		function bindtree() {
			var roleid = $('#t1').val();
			$('#mtree')
					.treegrid(
							{
								title : '模块授权',
								url : 'RoleManage/GetModule_Role?roleid='
										+ roleid,
								iconCls : 'icon-edit',
								height : 430,
								nowrap : true,
								rownumbers : true,
								animate : false,
								border : false,
								collapsible : false,
								singleSelect : true,
								remoteSort : false,
								idField : 'moduleid',
								treeField : 'modulename',
								columns : [ [
										{
											field : 'modulename',
											title : '名称',
											sortable : true,
											width : 260,
											formatter : function(val, rec) {
												return unescape(val)
											}
										},

										{
											field : 'actionstring',
											title : '权限',
											width : 800,
											formatter : function(val, rec) {
												var rtn = "";
												var arr = unescape(
														rec.actionstring)
														.split(',');
												var prr = unescape(
														rec.actionallowstring)
														.split(',');
												if (arr.length > 0) {
													for (var i = 0; i < arr.length; i++) {
														//获取角色的 权限值
														var pm = "";
														if (roleid) {
															//var flag = Xiaov.SmartWeb.Application.SysAdmin.Admin_Role.Check(rec.MODULEID, roleid, i);
															//判断i 是否在prr
															if (in_array(
																	arr[i], prr)
																	&& rec.actionallowstring != "")
																pm = "checked='checked' style='background-color:green;'";
														} else
															pm = "";
														rtn += "<input id='" + rec.moduleid + "$" + arr[i] + "' type='checkbox' value='" + i + "'" + pm + "/>"
																+ arr[i]
																+ "&nbsp;&nbsp;&nbsp;";
													}
												}
												if(rec.moduleid==1){
													console.log(rtn);
												}
												return rtn;
												//return unescape(rec.actionallowstring);
											}
										} ] ],
								onLoadSuccess : function() {

								}
							});

		}
		function in_array(str, arr) {
			// 得到needle的类型
			var type = typeof (str);

			if (type == "string" || type == "number") {
				for ( var i in arr) {
					if (arr[i] == str) {
						return true;
					}
				}
			}
			return false;
		}
	</script>
	<style scoped="scoped">
.textbox {
	height: 20px;
	margin: 0;
	padding: 0 2px;
	box-sizing: content-box;
}

/*.table tr {
        height: 15px;
    }*/
</style>
	<table id="tt"></table>
	<div id="add" class="easyui-window" closed="true" modal="true"
		maximized="true" title="角色信息" style="width: 600px;">
		<div class="easyui-tabs" id="tab">
			<div title="基本信息" style="padding: 10px">
				<form id="ssec-form" class="ssec-form">
					<table>
						<tr style="display: none;">
							<td>
								<div class="ssec-label" style="display: none;">角色ID：</div>
							</td>
							<td>
								<div class="ssec-text normal" style="display: none;">
									<input id="t1" type="text" />
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div class="ssec-label">角色名称：</div>
							</td>
							<td>
								<div class="ssec-text normal">
									<input id="t2" class="easyui-validatebox textbox"
										data-options="required:true" />
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div class="ssec-label">备注：</div>
							</td>
							<td>
								<div class="ssec-text normal">
									<input id="t3" class="easyui-validatebox textbox"
										data-options="required:true" />
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div class="ssec-label">启用禁用：</div>
							</td>
							<td><select id="t4" class="easyui-combobox"
								style="width: 100px;">
									<option value="1">启用</option>
									<option value="0">禁用</option>
							</select></td>
						</tr>
						<tr>
							<td>
								<div class="ssec-label">是否与其它角色共存：</div>
							</td>
							<td> <select
								id="cbTogether" class="easyui-combobox" style="width: 100px;">
									<option value="1">是</option>
									<option value="0">否</option>
							</select>
							</td>
						</tr>
						<tr>
							<td>
								<div class="ssec-label">排序：</div>
							</td>
							<td>
								<div class="ssec-text normal">
									<input id="t5" class="easyui-numberbox"
										data-options="required:true" />
								</div>
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div title="角色对应模块管理" style="padding: 0px">
				<table id="mtree"></table>
			</div>
		</div>
		<div style="padding: 5px; text-align: right;">
			<a class="easyui-linkbutton" onclick="save()" iconcls="icon-save">保存</a>
		</div>
	</div>
</body>
</html>
